package com.est.android.tsnLiveWallpaper;

import android.os.Handler;
import android.os.SystemClock;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;

public class TsnLiveWallpaper extends WallpaperService {

	@Override
	public void onCreate() {
		super.onCreate();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
	}
	
	@Override
	public Engine onCreateEngine() {
		return new TsnLiveWallpaperEngine();
	}

	class TsnLiveWallpaperEngine extends Engine 
		implements SharedPreferences.OnSharedPreferenceChangeListener {

		private final Handler mHandler = new Handler();

		private final Runnable mDrawSurface = new Runnable() {
			public void run() {
				drawFrame();
			}
		};

		private boolean mVisible;
		private SharedPreferences mPrefs;
		private float mOffset;
		private float mTouchX = -1;
		private float mTouchY = -1;
		private long mStartTime;
		private float mCenterX;
		private float mCenterY;
		private Paint mPaint = new Paint();
		
		private float[] curStringX = { 0, 0, 0};
		private float[] curStringY = { 0, 0, 0};
		
		@Override
		public void onSharedPreferenceChanged(
				SharedPreferences sharedPreferences, String key) {
			// TODO Auto-generated method stub
		}
		
		public TsnLiveWallpaperEngine() {
			mStartTime = SystemClock.elapsedRealtime();
			
			Typeface typeface = Typeface.createFromAsset(getAssets(), "DroidSans.ttf");
			
			mPaint.setColor(0xFFFFFFFF);
			mPaint.setTypeface(typeface);
		}
		
		@Override
		public void onCreate(SurfaceHolder surfaceHolder) {
			super.onCreate(surfaceHolder);
			setTouchEventsEnabled(true);
		}
		
		@Override
		public void onDestroy() {
			super.onDestroy();
			mHandler.removeCallbacks(mDrawSurface);
		}
		
		@Override
		public void onVisibilityChanged(boolean visible) {
			mVisible = visible;
			
			if(visible) {
				drawFrame();
			} else {
				mHandler.removeCallbacks(mDrawSurface);
			}
		}
		
		@Override
		public void onSurfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
			super.onSurfaceChanged(surfaceHolder, format, width, height);
            // store the center of the surface, so we can draw the cube in the right spot
            mCenterX = width/2.0f;
            mCenterY = height/2.0f;
            drawFrame();
		}
		
		@Override
		public void onSurfaceCreated(SurfaceHolder surfaceHolder) {
			super.onSurfaceCreated(surfaceHolder);
		}
		
		@Override
		public void onSurfaceDestroyed(SurfaceHolder surfaceHolder) {
			super.onSurfaceDestroyed(surfaceHolder);
			mVisible = false;
			mHandler.removeCallbacks(mDrawSurface);
		}
		
		@Override
		public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels) {
			mOffset = xOffset;
			drawFrame();
		}
		
		@Override
		public void onTouchEvent(MotionEvent event) {
			if(event.getAction() == MotionEvent.ACTION_MOVE) {
				mTouchX = event.getX();
				mTouchY = event.getY();
			} else {
				mTouchX = -1;
				mTouchY = -1;
			}
			super.onTouchEvent(event);
		}
		
		void drawFrame() {
			final SurfaceHolder surfaceHolder = getSurfaceHolder();
			final Rect frame = surfaceHolder.getSurfaceFrame();
			final int width = frame.width();
			final int height = frame.height();
			
			Canvas c = null;
			
			try {
				c = surfaceHolder.lockCanvas();
				if(c != null) {
					String[] testStrings = { "Test1", "Test2", "Test3" };
					
					c.drawColor(Color.BLACK);
					c.drawText(testStrings[0], curStringX[0], curStringY[0], mPaint);
					
					curStringX[0] = (float) (curStringX[0] + 1);
					curStringY[0] = (float) (curStringY[0] + 1);
				}
			} finally {
				if (c != null) {
					surfaceHolder.unlockCanvasAndPost(c);
				}
			}
			
			mHandler.removeCallbacks(mDrawSurface);
			
			if(mVisible) {
				mHandler.postDelayed(mDrawSurface, 1000 / 25);
			}
		}
	}
}
